The present invention relates to locking of computer resources.
When different computer entities such as computer processes or threads share a computer resource (for example, data, code, or a piece of hardware), it may be desirable to allow one of the computer entities to lock a resource for a while to prevent some types of access to the resource by other computer entities. For example, if two or more threads share computer data, and one thread has started but not finished to modify the data when another thread is accessing the data, the other thread may get incorrect information from the data and/or the data could be corrupted by the two threads. Also, if one thread has started but not finished execution of a critical code section when another thread starts executing the same code section, execution errors may occur if, for example, the critical code section modifies the state of a data area, a hardware controller, or some other computer resource. Therefore, locking techniques have been provided to allow computer entities to lock computer resources.
It is desirable to provide fast techniques for locking of computer resources.
The present invention provides methods and circuits that allow locking and unlocking of computer resources to be fast in many frequently occurring situations. In particular, in some embodiments, locking is typically fast when there is no contention for the lock (that is, the lock is not being held by another computer entity). Locking operations are also typically fast when the same computer entity, for example, the same thread, performs multiple lock operations on the same lock before the thread frees the lock. Multiple lock operations before the lock is freed can occur if the thread executes recursive code.
In some embodiments, the above advantages are achieved as follows. A computer processor includes a number of register pairs (LOCKADDR, LOCKCOUNT). Each LOCKADDR register is to hold a value that identifies a lock for a computer resource. In some embodiments, this value is a reference to a locked object. Thus, in some embodiments, the value is an address of a locked object. The corresponding LOCKCOUNT register holds the count of lock instructions associated with the lock identified by the LOCKADDR register. When a thread issues a lock instruction for the lock identified by the LOCKADDR register, the computer processor increments the corresponding LOCKCOUNT register. When the thread issues an unlock instruction, the computer processor decrements the corresponding LOCKCOUNT register.
In some embodiments, the processor is suitable for executing the lock and unlock instructions of the Java Virtual Machine. The Java Virtual Machine is described, for example, in T. Lindholm, F. Yellin, xe2x80x9cThe Java(copyright) Virtual Machine Specificationxe2x80x9d (1997). In the Java Virtual Machine, each object has a monitor associated with it. When a thread executes a lock instruction xe2x80x9cmonitorenterxe2x80x9d, a counter associated with the corresponding monitor is incremented. When the thread executes the unlock instruction xe2x80x9cmonitorexitxe2x80x9d, the counter is decremented. In some embodiments, the counters are implemented using the LOCKCOUNT registers.
In some embodiments, the LOCKCOUNT registers are omitted, and the lock for a resource is freed on any unlock instruction issued for the resource.
In some embodiments, each object includes a header which is a pointer to a class structure. The class structure is aligned on a 4-byte boundary, and hence the two LSBs of the pointer are zero and need not be stored in the header. Instead, the two LSBs of the header are used to store (1) a xe2x80x9cLOCKxe2x80x9d bit indicating whether the object is locked, and (2) a xe2x80x9cWANTxe2x80x9d bit indicating whether a thread is waiting to acquire a lock for the object.
Other features and advantages of the invention are described below. The invention is defined by the appended claims.